package org.duang.db.elasticsearch;

import org.duang.kit.ToolsKit;
import org.elasticsearch.action.index.IndexRequestBuilder;
import org.elasticsearch.client.Client;

public class ESearch extends ElasticSearchConnection {
	
	private static ESearch search;

	public ESearch() {
		searchConn = new ElasticSearchConnection();
	}

	public static ESearch getInstance() {
		if (null == search) {
			search = new ESearch();
		}
		return search;
	}
	
	public Client getESClient() {
		return client;
	}
	
	public String getElasticSearchIndexName() {
		return indexname;
	}
	
	public IndexRequestBuilder getDB(String indexName, String typeName) {
		try{
			return client.prepareIndex(indexName, typeName).setRefresh(true);
		} catch (Exception ex) {
			System.out.println("请检查是否在Duang.java里添加了ElasticSearchPlugin： " + ex.getMessage());
			return null;
		}
	}

	public void close() {
		if (ToolsKit.isNotEmpty(client)) {
			client.close();
		}
	}
}
